home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / clients / editres / geometry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  13.9 KB  |  493 lines

  1. /*
  2.  * $XConsortium: geometry.c,v 1.13 91/04/30 15:28:16 converse Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Chris D. Peterson, MIT X Consortium
  24.  */
  25.  
  26. #ifdef MSDOS
  27. #include "X11/Intrinsc.h"      /* QDK 05/11/1994 12:54pm. */
  28. #else
  29. #include "X11/Intrinsic.h"
  30. #endif
  31. #include <X11/StringDefs.h>
  32. #include <X11/Shell.h>
  33. #include <stdio.h>
  34.  
  35. #include <X11/Xaw/Cardinals.h>    
  36.  
  37. #include "editresP.h"
  38.  
  39. extern void SetMessage(), SetCommand(), SetAndCenterTreeNode(), AddString();
  40. extern void GetAllStrings(), InsertWidgetFromNode();
  41. extern int HandleXErrors();
  42. extern WNode * FindNode();
  43.  
  44. static WNode *FindWidgetFromWindow(), *FindWidgetFromWindowGivenNode();
  45. static void CreateFlashWidget(), FlashWidgets();
  46. static void AddToFlashList(), _AddToFlashList();
  47. static void FlashWidgetsOn(), FlashWidgetsOff(), FlashWidgetsCleanup();
  48.  
  49. /*    Function Name: _FindWidget
  50.  *    Description: Finds a widget in the tree and shows it to the user.
  51.  *    Arguments: w - any widget in the application.
  52.  *    Returns: none.
  53.  */
  54.  
  55. void 
  56. _FindWidget(w)
  57. Widget w;
  58. {
  59.     char msg[BUFSIZ];
  60.     WNode * node;
  61.     Window win, GetClientWindow();
  62.     int x, y;            /* location of event in root coordinates. */
  63.  
  64.     sprintf(msg, "Click on any widget in the client.\nEditres will %s",
  65.         "select that widget in the tree display.");
  66.  
  67.     SetMessage(global_screen_data.info_label, msg);
  68.  
  69.     if ( (win = GetClientWindow(w, &x, &y)) != None) {
  70.     node = FindWidgetFromWindow(global_tree_info, win);
  71.     if (node != NULL) {
  72.         ProtocolStream * stream = &(global_client.stream);        
  73.         
  74.         _XEditResResetStream(stream);
  75.         InsertWidgetFromNode(stream, node);
  76.         _XEditResPut16(stream, (short) x);
  77.         _XEditResPut16(stream, (short) y);
  78.         SetCommand(w, LocalFindChild, NULL);
  79.         return;
  80.     }
  81.     }
  82.  
  83.     SetMessage(global_screen_data.info_label, 
  84.       "That window does not appear to be\nin the currently displayed client.");
  85. }
  86.  
  87. /*    Function Name: FindWidgetFromWindow
  88.  *    Description: finds a widget in the current tree given its window id.
  89.  *    Arguments: tree_info - information about this tree.
  90.  *                 win - window to search for.
  91.  *    Returns: node - the node corrosponding to this widget.
  92.  */
  93.  
  94. static WNode * 
  95. FindWidgetFromWindow(tree_info, win)
  96. TreeInfo * tree_info;
  97. Window win;
  98. {
  99.     if (tree_info == NULL)
  100.     return(NULL);
  101.  
  102.     return(FindWidgetFromWindowGivenNode(tree_info->top_node, win));
  103. }
  104.  
  105. /*    Function Name: FindWidgetFromWindowGivenNode
  106.  *    Description: finds a widget in the current tree given its window id.
  107.  *    Arguments: node - current node.
  108.  *                 win - window to search for.
  109.  *    Returns: node - the node corrosponding to this widget.
  110.  */
  111.  
  112. static WNode *
  113. FindWidgetFromWindowGivenNode(node, win)
  114. WNode * node;
  115. Window win;
  116. {
  117.     int i;
  118.     WNode * ret_node;
  119.  
  120.     if (node->window == win)
  121.     return(node);
  122.  
  123.     for (i = 0; i < node->num_children; i++) {
  124.     ret_node = FindWidgetFromWindowGivenNode(node->children[i], win);
  125.     if (ret_node != NULL)
  126.         return(ret_node);
  127.     }
  128.     return(NULL);
  129. }
  130.  
  131. /*    Function Name: DisplayChild
  132.  *    Description: Displays the child node returned by the client
  133.  *    Arguments: event - the event from the client.
  134.  *    Returns: none.
  135.  */
  136.  
  137. void
  138. DisplayChild(event)
  139. Event * event;
  140. {
  141.     FindChildEvent * find_event = (FindChildEvent *) event;
  142.     WNode * node;
  143.     char msg[BUFSIZ];
  144.     void _FlashActiveWidgets();
  145.  
  146.     node = FindNode(global_tree_info->top_node, find_event->widgets.ids,
  147.             find_event->widgets.num_widgets);
  148.  
  149.     if (node == NULL) {
  150.     sprintf(msg, "Editres Internal Error: Unable to FindNode.\n");
  151.     SetMessage(global_screen_data.info_label, msg);
  152.     return;    
  153.     }
  154.  
  155.     SetAndCenterTreeNode(node);
  156.  
  157.     node = node->tree_info->top_node;
  158.  
  159.     sprintf(msg, "Widget Tree for client %s(%s).", node->name, node->class);
  160.     SetMessage(global_screen_data.info_label, msg);
  161.  
  162.     _FlashActiveWidgets(global_tree_info);
  163. }
  164.  
  165. /*    Function Name: _FlashActiveWidgets
  166.  *    Description: Highlights all active widgets in the tree.
  167.  *    Arguments: tree_info - information about the current tree.
  168.  *    Returns: none.
  169.  */
  170.  
  171. void
  172. _FlashActiveWidgets(tree_info)
  173. TreeInfo * tree_info;
  174. {
  175.     int i;
  176.     ProtocolStream * stream = &(global_client.stream);
  177.  
  178.     if (tree_info == NULL) {
  179.     SetMessage(global_screen_data.info_label,
  180.            "No widget Tree is avaliable.");
  181.     return;
  182.     }
  183.  
  184.     if (tree_info->num_nodes == 0) {
  185.     SetMessage(global_screen_data.info_label,"There are no active nodes.");
  186.     return;
  187.     }
  188.     
  189.     _XEditResResetStream(stream); 
  190.     /*
  191.      * Insert the number of widgets. 
  192.      */
  193.     _XEditResPut16(stream, (unsigned short) tree_info->num_nodes);
  194.  
  195.     for (i = 0; i < tree_info->num_nodes; i++) 
  196.     InsertWidgetFromNode(stream, global_tree_info->active_nodes[i]);
  197.  
  198.     SetCommand(tree_info->tree_widget, LocalFlashWidget, NULL);
  199. }
  200.  
  201. /*    Function Name: HandleFlashWidget
  202.  *    Description: Is called when client has returned geometry of all widget
  203.  *                   to flash.
  204.  *    Arguments: event - the event containing the client info.
  205.  *    Returns: none.
  206.  */
  207.  
  208. char *
  209. HandleFlashWidget(event)
  210. Event * event;
  211. {
  212.     GetGeomEvent * geom_event = (GetGeomEvent *) event;
  213.     char * errors = NULL;
  214.     int i;
  215.  
  216.     for (i = 0; i < (int)geom_event->num_entries; i++) 
  217.     AddToFlashList(global_tree_info, geom_event->info + i, &errors);
  218.  
  219.     FlashWidgets(global_tree_info);
  220.  
  221.     return(errors);
  222. }
  223.  
  224. /*    Function Name: AddWidgetToFlashList
  225.  *    Description: Adds a widget to the list of widget to flash.
  226.  *    Arguments: tree_info - info about this tree.
  227.  *                 geom_info - the info from the client about this widget.
  228.  *                 errors - a string containing the errors.
  229.  *    Returns: none
  230.  */
  231.  
  232. static void
  233. AddToFlashList(tree_info, geom_info, errors)
  234. TreeInfo * tree_info;
  235. GetGeomInfo * geom_info;
  236. char ** errors;
  237. {
  238.     WNode * node;
  239.     char buf[BUFSIZ];
  240.  
  241.     node = FindNode(tree_info->top_node, 
  242.             geom_info->widgets.ids, geom_info->widgets.num_widgets);
  243.  
  244.     if (node == NULL) {
  245.     sprintf(buf, "Editres Internal Error: Unable to FindNode.\n");
  246.     AddString(errors, buf); 
  247.     return;    
  248.     }
  249.  
  250.     if (geom_info->error) {
  251.     AddString(errors, geom_info->message); 
  252.     return;    
  253.     }
  254.  
  255.     if (!geom_info->visable) {
  256.     sprintf(buf, "%s(0x%lx) - This widget is not mapped\n",
  257.         node->name, node->id);
  258.     AddString(errors, buf); 
  259.     return;
  260.     }
  261.  
  262.     _AddToFlashList(tree_info, errors, node, 
  263.             geom_info->x, geom_info->y, 
  264.             geom_info->width + geom_info->border_width, 
  265.             geom_info->height + geom_info->border_width);
  266. }
  267.  
  268. /*    Function Name: _AddToFlashList
  269.  *    Description: adds the window to the current client's flash list.
  270.  *    Arguments: errors - a string to stuff any errors encountered.
  271.  *                 node - the node associated with this object.
  272.  *                 x, y - location of the flash widget in root coords.
  273.  *                 width, height - size of the flash widget.
  274.  *    Returns: none.
  275.  */
  276.  
  277. static void
  278. _AddToFlashList(tree_info, errors, node, x, y, width, height)
  279. TreeInfo * tree_info;
  280. char ** errors;
  281. WNode * node;
  282. int x, y;
  283. unsigned int width, height;
  284. {
  285.     Display * dpy = XtDisplay(tree_info->tree_widget);
  286.     Window window = (Window) node->window;
  287.     XWindowAttributes attrs;
  288.  
  289.     if (window == EDITRES_IS_OBJECT)
  290.     window = node->parent->window;
  291.  
  292.     if (window == EDITRES_IS_UNREALIZED) {
  293.     char buf[BUFSIZ];
  294.  
  295.     if (node->window == EDITRES_IS_OBJECT) 
  296.         sprintf(buf, "%s(0x%lx) - This object's parent is unrealized\n", 
  297.             node->name, node->id);        
  298.     else
  299.         sprintf(buf, "%s(0x%lx) - This widget is unrealized\n", 
  300.             node->name, node->id);
  301.  
  302.     AddString(errors, buf); 
  303.     return;
  304.     }
  305.  
  306.     global_error_code = NO_ERROR;                 /* Reset Error code. */
  307.     global_old_error_handler = XSetErrorHandler(HandleXErrors);
  308.     global_serial_num = NextRequest(dpy);
  309.  
  310.     XGetWindowAttributes(dpy, window, &attrs);
  311.  
  312.     XSync(dpy, FALSE);
  313.     XSetErrorHandler(global_old_error_handler);
  314.     if (global_error_code == NO_WINDOW) {
  315.     char buf[BUFSIZ];
  316.  
  317.     sprintf(buf, "%s(0x%lx) - This widget's window no longer exists.\n", 
  318.         node->name, node->id);
  319.     AddString(errors, buf); 
  320.     return;
  321.     }   
  322.  
  323.     if (attrs.map_state != IsViewable) {
  324.     char buf[BUFSIZ];
  325.  
  326.     sprintf(buf, "%s(0x%lx) - This widget is not mapped.\n",
  327.         node->name, node->id);
  328.     AddString(errors, buf); 
  329.     return;
  330.     }   
  331.  
  332.     CreateFlashWidget(tree_info, x, y, width, height);
  333. }
  334.  
  335. /*    Function Name: CreateFlashWidget
  336.  *    Description: Creates a widget of the size specified that
  337.  *                   will flash on the display, and adds it to the list
  338.  *                   of widgets to flash.
  339.  *    Arguments: tree_info - the tree information structure.
  340.  *                 x,y,width, height - size and location of the flash widget.
  341.  *    Returns: none.
  342.  */
  343.     
  344. #define MORE_FLASH_WIDGETS 5
  345.  
  346. static void
  347. CreateFlashWidget(tree_info, x, y, width, height)
  348. TreeInfo * tree_info;
  349. int x, y;
  350. unsigned int width, height;
  351. {
  352.     Widget shell;
  353.     Arg args[3];
  354.     Cardinal num = 0;
  355.     Dimension bw;
  356.  
  357.     XtSetArg(args[num], XtNx, x); num++;
  358.     XtSetArg(args[num], XtNy, y); num++;
  359.     XtSetArg(args[num], XtNbackground, global_resources.flash_color); num++;
  360.  
  361.     shell = XtCreatePopupShell("flash", overrideShellWidgetClass, 
  362.                    tree_info->tree_widget, args, num);
  363.  
  364.     num = 0;
  365.     XtSetArg(args[num], XtNborderWidth, &bw); num++;
  366.     XtGetValues(shell, args, num);
  367.     
  368.     bw *= 2;
  369.  
  370.     num = 0;
  371.     XtSetArg(args[num], XtNwidth, (width - bw)); num++;
  372.     XtSetArg(args[num], XtNheight, (height - bw)); num++;
  373.     XtSetValues(shell, args, num);    
  374.     
  375.     if (tree_info->num_flash_widgets + 1 > tree_info->alloc_flash_widgets) {
  376.     tree_info->alloc_flash_widgets += MORE_FLASH_WIDGETS;
  377.     tree_info->flash_widgets =
  378.         (Widget *) XtRealloc((char *)tree_info->flash_widgets,
  379.                   sizeof(Widget) * tree_info->alloc_flash_widgets);
  380.     }
  381.  
  382.     tree_info->flash_widgets[tree_info->num_flash_widgets] = shell;
  383.     tree_info->num_flash_widgets++;
  384. }
  385.  
  386. /*    Function Name: FlashWidgets
  387.  *    Description: Starts the widgets flashing.
  388.  *    Arguments: tree_info - the info about the tree (contains flash list)
  389.  *    Returns: none
  390.  */
  391.  
  392. static void
  393. FlashWidgets(tree_info)
  394. TreeInfo * tree_info;
  395. {
  396.     int i;
  397.     unsigned long wait, half_flash;
  398.     XtAppContext ac = XtWidgetToApplicationContext(tree_info->tree_widget);
  399.  
  400.     if (tree_info->flash_widgets == NULL) /* no widgets to flash. */
  401.     return;
  402.  
  403.     wait = half_flash = global_resources.flash_time/2;
  404.     for (i = 1; i < global_resources.num_flashes; i++) {
  405.     XtAppAddTimeOut(ac, wait, FlashWidgetsOff,(XtPointer)tree_info);
  406.     wait += half_flash;
  407.     XtAppAddTimeOut(ac, wait, FlashWidgetsOn,(XtPointer)tree_info);
  408.     wait += half_flash;
  409.     }
  410.  
  411.     wait += half_flash;
  412.     XtAppAddTimeOut(ac, wait, FlashWidgetsCleanup, (XtPointer)tree_info);
  413.  
  414.     FlashWidgetsOn((XtPointer) tree_info, (XtIntervalId *) NULL);
  415. }
  416.     
  417. /*    Function Name: FlashWidgetsOn
  418.  *    Description: Turns on all the Flash Widgets.
  419.  *    Arguments: info_ptr - pointer to the tree info.
  420.  *                 id - *** UNUSED ***.
  421.  *    Returns: none
  422.  */
  423.  
  424. /* ARGSUSED */
  425. static void
  426. FlashWidgetsOn(info_ptr, id)
  427. XtPointer info_ptr;
  428. XtIntervalId * id;
  429. {
  430.  
  431.     int i;
  432.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  433.     
  434.     for (i = 0; i < tree_info->num_flash_widgets; i++) {
  435.     XtRealizeWidget(tree_info->flash_widgets[i]);
  436.     XMapRaised(XtDisplay(tree_info->flash_widgets[i]),
  437.            XtWindow(tree_info->flash_widgets[i]));
  438.     }
  439. }
  440.  
  441. /*    Function Name: FlashWidgetsOff
  442.  *    Description: Turns off all the Flash Widgets.
  443.  *    Arguments: info_ptr - pointer to the tree info.
  444.  *                 id - *** UNUSED ***.
  445.  *    Returns: none
  446.  */
  447.  
  448. /* ARGSUSED */
  449. static void
  450. FlashWidgetsOff(info_ptr, id)
  451. XtPointer info_ptr;
  452. XtIntervalId * id;
  453. {
  454.     int i;
  455.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  456.     
  457.     for (i = 0; i < tree_info->num_flash_widgets; i++)
  458.     XtUnmapWidget(tree_info->flash_widgets[i]);
  459. }
  460.  
  461. /*    Function Name: FlashWidgetsCleanup
  462.  *    Description: Destroys all the Flash Widgets.
  463.  *    Arguments: info_ptr - pointer to the tree info.
  464.  *                 id - *** UNUSED ***.
  465.  *    Returns: none
  466.  */
  467.  
  468. /* ARGSUSED */
  469. static void
  470. FlashWidgetsCleanup(info_ptr, id)
  471. XtPointer info_ptr;
  472. XtIntervalId * id;
  473. {
  474.     int i;
  475.     TreeInfo * tree_info = (TreeInfo *) info_ptr;
  476.  
  477. /*
  478.  * Unmap 'em first for consistency.
  479.  */
  480.     
  481.     for (i = 0; i < tree_info->num_flash_widgets; i++)
  482.     XtUnmapWidget(tree_info->flash_widgets[i]);
  483.  
  484.     XFlush(XtDisplay(tree_info->tree_widget));
  485.  
  486.     for (i = 0; i < tree_info->num_flash_widgets; i++) 
  487.     XtDestroyWidget(tree_info->flash_widgets[i]);
  488.  
  489.     XtFree((char *)tree_info->flash_widgets);
  490.     tree_info->flash_widgets = NULL;
  491.     tree_info->num_flash_widgets = tree_info->alloc_flash_widgets = 0;
  492. }
  493.